﻿using BlueportCoffee.SocketModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

namespace BlueportCoffee.model
{
    public class IOMsgShow
    {
        private static IOMsgShow instance;
        private IOMsgShow() { }

        public static IOMsgShow Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IOMsgShow();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool AllBol = true;
        public bool RobotBol = true;
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        /// <summary>
        ///  IO的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        public static bool IsMsgError = true;
        public bool isRobotBol = false;
        string nostrs = "";
        public Thread All_thr;
   
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.PortNameShow)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                IOSerialPortShow.Instance.IceMachineConnect(IPorPortInfo.Instance.PortNameShow, 38400, "", 8, 1);//其他串口通讯
                IOSerialPortShow.Instance.serialPort.DataReceived += SerialPort_DataReceived;
                All_thr = new Thread(AllMessages);
                All_thr.IsBackground = true;
                All_thr.Start();
            }
            catch (Exception)
            {
                IOSerialPortShow.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        public void AllMessages()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (Instance.AllBol)
                            IOSerialPortShow.Instance.SerialSend(DigitalMsg.Instance.AllMessage);
                        Thread.Sleep(100);
                    }
                    catch (Exception)
                    {
                        Thread.Sleep(100);
                    }

                }
            }
            catch (Exception)
            {
                IOSerialPortShow.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                var dd = IOSerialPortShow.Instance.serialPort.BytesToRead;
                byte[] result = new byte[dd];
                IOSerialPortShow.Instance.serialPort.Read(result, 0, dd);
                string str = "";// $"{DateTime.Now}：\n";
                str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void DigitalMsgBol2(string str)
        {
            try
            {
                var Bol = str.Split(' ');
                bool bol = false;
                if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol1 != (Bol[4] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol1 = Bol[4] == "01" ? true : false;

                if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol2 != (Bol[6] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol2 = Bol[6] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol3 != (Bol[8] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol3 = Bol[8] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol4 != (Bol[10] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol4 = Bol[10] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.SealingMachine_Sel_Bol != (Bol[12] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.SealingMachine_Sel_Bol = Bol[12] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.LabelingMachine_Sel_Bol != (Bol[14] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.LabelingMachine_Sel_Bol = Bol[14] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol1 != (Bol[16] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol1 = Bol[16] == "01" ? true : false;

                if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol2 != (Bol[18] == "01" ? true : false))
                {
                    //if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                    //    isEmail = true;
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol2 = Bol[18] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.CupCapacity_Sel_Bol1 != (Bol[20] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.CupCapacity_Sel_Bol1 = Bol[20] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.CupCapacity_Sel_Bol2 != (Bol[22] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.CupCapacity_Sel_Bol2 = Bol[22] == "01" ? true : false;

                if (WuLiaoCoffeeSel.Instance.LidCapacity_Sel_Bol1 != (Bol[24] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.LidCapacity_Sel_Bol1 = Bol[24] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.WastewaterBucket_Sel_Bol != (Bol[26] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.WastewaterBucket_Sel_Bol = Bol[26] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol1 != (Bol[28] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Milk_Sel_Bol1 = Bol[28] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.EarlyWarningMilk_Sel_Bol1 != (Bol[30] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.EarlyWarningMilk_Sel_Bol1 = Bol[30] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol2 != (Bol[32] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Milk_Sel_Bol2 = Bol[32] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.EarlyWarningMilk_Sel_Bol2 != (Bol[34] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.EarlyWarningMilk_Sel_Bol2 = Bol[34] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.LBQFK1_Sel_Bol != (Bol[36] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.LBQFK1_Sel_Bol = Bol[36] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.LBQFK2_Sel_Bol != (Bol[38] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.LBQFK2_Sel_Bol = Bol[38] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.LGQFK1_Sel_Bol != (Bol[40] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.LGQFK1_Sel_Bol = Bol[40] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.CapPressingDown_Sel_Bol != (Bol[42] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.CapPressingDown_Sel_Bol = Bol[42] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.CapPressingUP_Sel_Bol != (Bol[44] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.CapPressingUP_Sel_Bol = Bol[44] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.CoffeeWeight_Sel_Bol != (Bol[48] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.CoffeeWeight_Sel_Bol = Bol[48] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.MilkWeight_Sel_Bol != (Bol[50] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.MilkWeight_Sel_Bol = Bol[50] == "01" ? true : false;
                if (bol)
                {
                    ClientList clientList = new ClientList();
                    clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                    clientList.code = 2;
                    SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public delegate void TestDelegateInvoke();
        //private void DelegateInvokeMethod()
        //{
        //    try
        //    {
        //        EmailShow emailShow = new EmailShow();
        //        emailShow.content = MainWindow.EmailError();
        //        var saves1 = JsonConvert.SerializeObject(emailShow);
        //        Console.WriteLine(emailShow.content);
        //        var dd1 = HttpMsg.Instance.sendEmailMsg(saves1);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        string strlist = "";
        public void MsgShow(string str)
        {
            try
            {
                // Console.WriteLine(str);
                nostrs = "";
                var nostr = str.Split(' ');
                if (nostr.Length >= 2)
                    nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                for (int i = 1; i < nostr.Count() - 1; i++)
                {
                    nostrs += nostr[i] + " ";
                }
                if (nostrs.Length >= 5)
                {
                    if (nostrs.Substring(0, 5) == "01 05")
                    {
                        Console.WriteLine(str);
                    }
                    else if (nostrs.Substring(0, 5) == "01 04")
                    {
                        strlist = nostrs;
                    }
                    else if (nostrs.Substring(0, 5) == "03 02")
                    {
                        Console.WriteLine(nostrs);
                        var robstr = nostrs.Split(' ');
                        RobotBol = robstr[3] == "01" ? true : false;
                        Console.WriteLine(robstr[3] == "01" ? true : false);
                    }
                    else
                    {
                        if (strlist != "")
                            strlist += nostrs;
                    }
                }
                if (strlist.Split(' ').Count() >= 105)
                {
                    DigitalMsgBol2(strlist);
                    strlist = "";
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

        #endregion
    }
}
